import {
	judgeIosPermission,
	requestAndroidPermission,
	checkSystemEnableLocation,
	gotoAppPermissionSetting
} from "@/js_sdk/wa-permission/permission.js";
var calanderURL = "content://com.android.calendar/calendars";
var calanderEventURL = "content://com.android.calendar/events";
var calanderRemiderURL = "content://com.android.calendar/reminders";
var calanderExtendedPropertiesURL = "content://com.android.calendar/extendedproperties";


var calId;

// 获取手机平台
const getplatform =async function(data) {
	//判断是否获取权限
	if (! await requestAndroidPermissionFn()) return false;
	uni.getSystemInfo({
		success: res => {
			if (res.platform === 'android') {
				addEvent(data)
			}
		}
	})
	return true;
}


const addCalendar =async function (data){
let a = await requestAndroidPermission('android.permission.READ_CALENDAR');
let b = await requestAndroidPermission('android.permission.WRITE_CALENDAR');
if(a == 1 && b == 1){
	addEvent(data)
}
}

// vue的method里编写如下代码
const requestAndroidPermissionFn = async function() {
	if (await requestAndroidPermission('android.permission.READ_CALENDAR') != 1 ||
		await requestAndroidPermission('android.permission.WRITE_CALENDAR') != 1) {
		uni.showToast({
			title: "calendar authorization not obtained"
		});
		return false;
	}
	return true;
}

const addEvent = function(data) {
	var Uri = plus.android.importClass("android.net.Uri");
	var main = plus.android.runtimeMainActivity();
	var userCursor = plus.android.invoke(main.getContentResolver(), "query", Uri.parse(calanderURL), null, null,
		null, null);
	plus.android.invoke(userCursor, "moveToLast");
	calId = plus.android.invoke(userCursor, "getString", plus.android.invoke(userCursor, "getColumnIndex",
		"_id"));
	var ContentValues = plus.android.importClass("android.content.ContentValues");
	var events = new ContentValues();
	events.put("title", data.title);
	events.put("description", data.description);
	// 插入账户  
	events.put("calendar_id", calId);
	//位置  可不填
	// events.put("eventLocation", "位置");
	events.put("dtstart", data.dtstart); //时间戳 到毫秒的时间戳
	events.put("dtend", data.dtend); //时间戳 到毫秒的时间戳
	events.put("hasAlarm", 1);
	events.put("eventTimezone", "Asia/Shanghai"); // 这个是时区，必须有，这个就是中国标准时区，在中国境内的不必再改  
	// HAS_EXTENDED_PROPERTIES
	// events.put("allDay", 1); 




	// 添加事件    
	var newEvent = plus.android.invoke(plus.android.runtimeMainActivity().getContentResolver(), "insert", Uri.parse(
		calanderEventURL), events);
	// 事件提醒的设定  
	var id = plus.android.invoke(newEvent, "getLastPathSegment");
	var values = new ContentValues();
	values.put("event_id", id);
	// 提前120分钟有提醒  
	values.put("minutes", "120");
	values.put("method", "1");


	//添加额外参数
	var extendedProperties = new ContentValues();
	extendedProperties.put("event_id", id);
	extendedProperties.put("name", "agenda_info");
	extendedProperties.put("value", {"need_alarm":true});


	plus.android.invoke(main.getContentResolver(), "insert", Uri.parse(calanderRemiderURL), values);
	plus.android.invoke(main.getContentResolver(), "insert", Uri.parse(calanderRemiderURL),
		extendedProperties);


	uni.showToast({
		title: 'calendar event reminder added',
		icon: 'none', //success 等
		duration: 1000
	});
}

export {
	requestAndroidPermission,
	addEvent,
	getplatform,
	addCalendar
}